<html>
<head><meta charset="utf-8"><title>False positive · miri · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/index.html">miri</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html">False positive</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239196915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239196915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lander Brandt <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239196915">(May 18 2021 at 04:34)</a>:</h4>
<p>Hi folks, I'm running Miri on some internal tests that unfortunately I cannot share, but I'm seeing strange behavior that makes me think the Miri error being triggered is a false positive.</p>
<p>The error is: <code>no item granting read access to tag &lt;untagged&gt; at &lt;alloc&gt; found in borrow stack.</code></p>
<p>This happens when attempting to do something like:</p>
<div class="codehilite"><pre><span></span><code>let src = &amp;self.data.first_field as *const _ as *const u8;
ptr.copy_from(src, byte_count);
</code></pre></div>
<p>If I do the following the error goes away:</p>
<div class="codehilite"><pre><span></span><code>println!(&quot;Data len: {}&quot;, std::mem::size_of_val(&amp;self.data));
let src = &amp;self.data.first_field as *const _ as *const u8;
ptr.copy_from(src, byte_count);
</code></pre></div>
<p>Anyone know if there's an open issue for this item or why I may be seeing this behavior?</p>



<a name="239197061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239197061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239197061">(May 18 2021 at 04:36)</a>:</h4>
<p>It's late at my TZ but I'm going to drop stuff just in case it helps.</p>



<a name="239197130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239197130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239197130">(May 18 2021 at 04:37)</a>:</h4>
<p>its kinda hard to know what's happening without a minimal example. That being said, could it be that your <code>println</code> call is creating a lifetime region where <code>self.data</code> can be read?</p>



<a name="239197332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239197332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lander Brandt <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239197332">(May 18 2021 at 04:41)</a>:</h4>
<p>I don't think so. If I put the <code>println</code> in its own scope I get the successful behavior (as if it was not in its own scope). I tried to create explicit references to <code>self.data</code> and get a reference to <code>first_field</code> from there and those fail as well.</p>



<a name="239197406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239197406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lander Brandt <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239197406">(May 18 2021 at 04:43)</a>:</h4>
<p>And I know, I'm sorry for not being able to provide a minimal example. I'm relatively new to this codebase and thought hacking it up in the playground for a min PoC may take too long if it's a known issue. I'll see about providing one tomorrow though</p>



<a name="239198667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239198667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lander Brandt <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239198667">(May 18 2021 at 05:03)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=35dcb91a4a334fe731786f960aa2f468">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=35dcb91a4a334fe731786f960aa2f468</a></p>
<p>Here's a repro. if you uncomment the <code>println</code> the code works.</p>



<a name="239199258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239199258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239199258">(May 18 2021 at 05:13)</a>:</h4>
<p>It's not a bug, but rather a limitation of the current stacked borrows model. This:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="p">(</span><span class="n">larger_data</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">).</span><span class="n">copy_from</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">identifier</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="mi">4</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>is undefined behavior because <code>&amp;data.identifier</code> only has access to that one byte and you are writing 4 bytes into it. However, if a pointer is created with access to all of <code>data</code>, then SB cannot exclude the possibility that your <code>&amp;data.identifier as *const _</code> pointer was actually derived from that other pointer which does have access to the entire allocation, so it is considered defined behavior. Ideally, both versions would be undefined behavior, but distinguishing pointers (i.e. pointer provenance) is a lot harder than references (which use the normal rust borrowing model).</p>



<a name="239199447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239199447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239199447">(May 18 2021 at 05:16)</a>:</h4>
<p>To fix the UB, you should use <code>addr_of!</code> like so:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">data_ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">Data</span><span class="o">&lt;</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">;</span><span class="w"> </span><span class="c1">// this way the pointer has access to the whole of data</span>
<span class="w">        </span><span class="p">(</span><span class="n">larger_data</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">).</span><span class="n">copy_from</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">addr_of</span><span class="o">!</span><span class="p">((</span><span class="o">*</span><span class="n">data_ptr</span><span class="p">).</span><span class="n">identifier</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="c1">// create a derived pointer with access to all of data</span>
<span class="w">            </span><span class="mi">4</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="239199768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239199768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lander Brandt <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239199768">(May 18 2021 at 05:22)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> nice, this makes sense. I changed the code to grab a pointer to the parent object and cast that as a  <code>*const u8</code> and the code now works. Thank you for your help.</p>



<a name="239289781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239289781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Ellis O&#x27;Riley Jr. <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239289781">(May 18 2021 at 17:06)</a>:</h4>
<p>Question.  I know const is for constant.  What’s *const?</p>



<a name="239292564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/False%20positive/near/239292564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/False.20positive.html#239292564">(May 18 2021 at 17:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="344328">Charles Ellis O'Riley Jr.</span> <a href="#narrow/stream/269128-miri/topic/False.20positive/near/239289781">said</a>:</p>
<blockquote>
<p>Question.  I know const is for constant.  What’s *const?</p>
</blockquote>
<p>is a raw pointer: there is <code>*const T</code> and <code>*mut T</code>. Neither of them have lifetime restrictions and dereferencing them is unsafe. The difference is that you cannot write to a <code>*const T</code> directly but you can write to a <code>*mut T</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>